package com.example.readexceldata.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class UserServiceImpl {

    public String sendGetRequestWithTokenAndParams(String url, String token, String paramKey, String paramValue) throws Exception {
        // 构建带有参数的 URL
//        String fullUrl = url + "?" + URLEncoder.encode(paramKey, StandardCharsets.UTF_8) + "=" + URLEncoder.encode(paramValue, StandardCharsets.UTF_8);

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", token)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
public String sendPutRequestWithTokenAndBody(String url, String token, String jsonBody) throws IOException {
        OkHttpClient client = new OkHttpClient();


        RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json; charset=utf-8"));

        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", token)
                .put(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    public static void main(String[] args) {
        UserServiceImpl service = new UserServiceImpl();
        try {
            String url = "http://120.234.108.204:8081/api/user?name=&username=&phone=&type=&organizationCode=&workstationId=&start=2024-12-16&end=2024-12-17&page=1&size=99999&sort=createdDate%7Cdesc";
            String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJES1NCIiwiZXhwIjoxNzM0NTAwNDQwLCJpcCI6IjE3Mi4yMS4wLjEiLCJyb2xlIjoiQURNSU4iLCJvcmdhbml6YXRpb25OYW1lIjoiIiwibmFtZSI6IueuoeeQhuWRmCIsIndvcmtzdGF0aW9uTmFtZSI6IiIsInR5cGUiOiLlhazlrokifQ.Uo3a3njqW3HG-EGgnbtxKUqWSOF8tuHPLqykN0_PUr3ck5Jk5_MprINZbF2-S4uRNw1j2OZbe912RBJshDIiYg";
            String paramKey = "param1";
            String paramValue = "value1";
            String response = service.sendGetRequestWithTokenAndParams(url, token, paramKey, paramValue);
            JSONObject object = JSON.parseObject(response);
            JSONArray array = object.getJSONArray("data");
            int i=0;
            boolean flag = false;
            for (Object o : array) {
                //获取指定值
                JSONObject jsonObject = (JSONObject) o;
                String username = jsonObject.get("username").toString();
                System.out.println(jsonObject.get("username"));
                     i++;
                 if(username.equals("0febad4c7a5bd192cbd8552f4a55e938"))
                 {
                     flag = true;
                 }
                 if(flag)
                 {
                     String putResponse = service.sendPutRequestWithTokenAndBody("http://120.234.108.204:8081/api/user/resetPassword/"+username,token,jsonObject.toJSONString());
                    System.out.println("PUT Response:"+putResponse);
                    Thread.sleep(3000);
                 }

            }
            System.out.println("一共多少数据："+i+"条");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
